import { getPurchased } from '@/apis/index';
import { errorReport, DatePlus } from '@/utils';
import { List } from '@/types/purchased';
import { ref } from 'vue';
import { Single } from '@/types/api.d';

export default (errPostion: string) => {
  const list = ref<List[]>([]);
  const getPurchasedList = () => {
    getPurchased()
      .then(res => {
        if (res.status_code) {
          throw new Error(`status_code：${res.status_code}`);
        }
        if (res.data) {
          list.value = res.data.seq_list.map(
            ({ stock_code, stock_name, market, rdate, enddate, sid, has_auth }) => ({
              stockCode: stock_code,
              stockName: stock_name,
              marketId: market,
              startDate: new DatePlus(rdate).format('yyyy-MM-dd'),
              endDate: new DatePlus(enddate).format('yyyy-MM-dd'),
              isPrime: sid === Single.primeSid,
              hasAuth: has_auth === '1',
            })
          );
        }
      })
      .catch(error => {
        errorReport({
          name: `${errPostion}中的getPurchasedList报错,${error.name}`,
          message: error.message,
          stack: error.stack,
        });
      });
  };
  return {
    getPurchasedList,
    list,
  };
};
